package com.ineast.common.response;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 返回结果公共类
 *
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 5859490173860051065L;

    private Integer code;

    private String message;

    private T data;


    public static <T> Result<T> success(Boolean success,ResultCode resultCode,T data) {
        Result<T> result = new Result<>();
//        result.setSuccess(success);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
//        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    /** 总记录数 */
    private long total;

    /**
     * 构造方法私有化，里面的方法都是静态方法，保护属性防篡改
     */
    public Result() {
        this.data = null;
    }

    /**
     * 成功静态方法
     *
     * @return Result
     */
    public static <T> Result<T> ok() {
        Result<T> result = new Result<>();
//        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    /**
     * 成功静态方法（分页查询返回总记录数）
     *
     * @return Result
     */
    public static <T> Result<T> ok(Long total) {
        Result<T> result = new Result<>();
//        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setTotal(total);
        return result;
    }


    /**
     * 失败静态方法
     *
     * @return Result
     */
    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
//        result.setSuccess(false);
        result.setCode(ResultCode.ERROR.getCode());
        result.setMessage(ResultCode.ERROR.getMessage());
        return result;
    }

    /**
     * 成功返回状态，带参数
     *
     * @param success 状态
     * @return Result
     */
    public Result<T> success(Boolean success) {
//        this.setSuccess(success);
        return this;
    }

    /**
     * 自定义消息返回
     *
     * @param message 消息
     * @return Result
     */
    public Result<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    /**
     * 自定义状态码返回
     *
     * @param code 状态码
     * @return Result
     */
    public Result<T> code(Integer code) {
        this.setCode(code);
        return this;
    }

    /**
     * 数据返回 K/V
     *
     * @param key   key
     * @param value value
     * @return Result
     */
    public Result<T> data(String key, T value) {
        Map map = null;
        if (this.data != null){
            map = JSONObject.parseObject(JSONObject.toJSONString(this.data), Map.class);
        } else {
            map = new HashMap();
        }
        map.put(key, value);
        this.data((T) map);
        return this;
    }

    /**
     * 数据返回 Map 对象
     *
     * @param obj T Object
     * @return Result
     */
    public Result<T> data(T obj) {
        this.setData(obj);
        return this;
    }
    public Result<T> total(long obj) {
        this.setTotal(obj);
        return this;
    }
}

